layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 就创模范管理
     */
    var LabourModelCategory = {
        tableId: "labourModelCategoryTable"
    };

    /**
     * 初始化表格的列
     */
    LabourModelCategory.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'typeName', sort: true, title: '名称'},
            {field: 'icons', sort: true, title: '图标',templet:LabourModelCategory.formatImg},
            {field: 'type', sort: true, title: '类型',templet:LabourModelCategory.formatType},
            {field: 'sortNo', sort: true, title: '排序'},
            {field: 'status', sort: true, title: '状态',templet:LabourModelCategory.formatStatus},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    //图片
    LabourModelCategory.formatImg = function (d){
        if (!d.icons) {
            return '';
        }
        return '<img class="layui-upload-img" style="height: 100px;width: 100px" src="' + d.icons + '">'
    }

    //状态
    LabourModelCategory.formatStatus = function (d) {
        if (d.status == null) return "";
        let checkedStatus = d.status == 1 ? 'checked' : '';
        return "<input type='checkbox'  " + checkedStatus + " value='" + d.status + "' lay-skin='switch' lay-text='启用|禁用' lay-filter='statusOptFilter' successId='" + d.id + "'>";
    };

    //类型处理
    LabourModelCategory.formatType = function (d) {
        if (d.type == "1") {
            return "就业模范"
        } else if (d.type == "2") {
            return "创业模范";
        } else {
            return "";
        }
    }

    /**
     * 点击查询按钮
     */
    LabourModelCategory.search = function () {
        var queryData =getParam();

        table.reload(LabourModelCategory.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
    * 获取查询参数
    */
    function getParam(){
      var queryData = {};
      queryData['type'] = $('#type').val();
      queryData['typeName'] = $('#typeName').val();
      return queryData;
    }

    /**
     * 弹出添加对话框
     */
    LabourModelCategory.openAddDlg = function () {
        func.open({
            title: '添加就创模范',
            content: Changein.ctxPath + '/labourModelCategory/add',
            tableId: LabourModelCategory.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      LabourModelCategory.openEditDlg = function (data) {
          func.open({
              title: '修改就创模范',
              content: Changein.ctxPath + '/labourModelCategory/edit?id=' + data.id,
              tableId: LabourModelCategory.tableId
          });
      };


   /**
     * 删除
     */
    var deleteBatchLock = false;
    LabourModelCategory.deleteBatch = function () {
        if (!deleteBatchLock) {
            var checkRows = table.checkStatus(Sample.tableId);
            if (checkRows.data.length == 0) {
                Changein.error("请选择要删除的数据");
                return false;
            }
            var arr = [];
            var tabData = checkRows.data;
            for (var i = 0; i < tabData.length; i++) {
                if (tabData[i].audit != 1) {
                    arr.push(tabData[i].id);
                }
            }
            if (arr.length == 0) {
                Changein.error("选中数据全部已审核，请选择未审核的数据");
                return false;
            }
            var operation = function () {
                deleteBatchLock = true;
                var ajax = new $ax(Changein.ctxPath + "/labourModelCategory/batchDeleteByIds", function (data) {
                    if (data.code != 200) {
                        Changein.error("删除失败！" + data.message);
                    } else {
                        Changein.success("删除成功！");
                    }
                    deleteBatchLock = false;
                    table.reload(LabourModelCategory.tableId);
                }, function (data) {
                    deleteBatchLock = false;
                    Changein.error("删除失败！" + data.responseJSON.message)
                });
                ajax.set({"ids": arr.toString()});
                ajax.start();
            }
            Changein.confirm("是否删除?", operation);
        } else {
            Changein.error("正在删除中");
        }
    };


    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    LabourModelCategory.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Changein.ctxPath + "/labourModelCategory/delete", function (data) {
                Changein.success("删除成功!");
                table.reload(LabourModelCategory.tableId);
            }, function (data) {
                Changein.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Changein.confirm("是否删除?", operation);
    };

     /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    var deleteLock = false;
    LabourModelCategory.onDeleteItem = function (data) {
        if(data.audit == 1){
            Changein.error("选中数据已审核，请选择未审核的数据");
            return false;
        }
        var operation = function () {
            deleteLock = true;
            var ajax = new $ax(Changein.ctxPath + "/labourModelCategory/delete", function (data) {
                deleteLock = false;
                if(data.code != 200){
                    Changein.error("删除失败！" + data.message);
                }else{
                    Changein.success("删除成功！");
                }
                table.reload(LabourModelCategory.tableId);
            }, function (data) {
                deleteLock = false;
                Changein.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        if(!deleteLock){
            Changein.confirm("是否删除?", operation);
        }else{
            Changein.error("正在删除中");
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + LabourModelCategory.tableId,
        url: Changein.ctxPath + '/labourModelCategory/list',
        page: true,
        height: "full-130",
        cellMinWidth: 100,
        cols: LabourModelCategory.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        LabourModelCategory.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        LabourModelCategory.openAddDlg();
    });
   // 删除
    $("#btnDel").click(function () {
        LabourModelCategory.deleteBatch();
    });

    // 工具条点击事件
    table.on('tool(' + LabourModelCategory.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            LabourModelCategory.openEditDlg(data);
        } else if (layEvent === 'delete') {
            LabourModelCategory.onDeleteItem(data);
        }
    });

    //状态修改
    form.on("switch(statusOptFilter)", function (data) {

        let id = $(data.elem).attr("successId");
        let status = 0;
        if (data.elem.checked) {
            status = 1;
        }
        let param = {id: id, status: status};
        $.ajax({
            url: Changein.ctxPath + '/labourModelCategory/editItem',
            data: param,
            type: "POST",
            success: function (result) {
                if (result && result.code == 200) {
                    if (data.elem.checked) {
                        Changein.success("启用成功！");

                    } else {
                        Changein.success("禁用成功！");
                    }
                    initTable.reload();
                } else {
                    Changein.error("操作失败！");
                }
            }, dataType: "json"
        });
    });
});
